//
//  RegisterNextViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/14.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "RegisterNextViewController.h"
#import "StartYQViewController.h"

@interface RegisterNextViewController ()
{
    NSString * sexFlag;
    
    NSMutableString * passWordStr;
//    NSMutableString * nameStr;

}
@end

@implementation RegisterNextViewController
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
   
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

   }
- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
    // Do any additional setup after loading the view.
}
-(void)main
{
    passWordStr = [[NSMutableString alloc]init];
//    nameStr = [[NSMutableString alloc]init];

    //self.view.backgroundColor = [UIColor whiteColor];
    //背景图
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView * bgImgV = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    bgImgV.image = [UIImage imageNamed:@"登录"];
    [self.view addSubview:bgImgV];
    //上面2
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 20)];
    titleLab.text = @"注册";
    titleLab.textColor = UIColorFromRGB(0xFFFFFF);
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.font = [UIFont systemFontOfSize:23];
    [self.view addSubview:titleLab];
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 30, 30, 20);
    [leftBtn setImage:[UIImage imageNamed:@"上月"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnActoin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftBtn];
    
    
    //textField
    NSArray * imgArr = @[@"",@"密码icon",@"姓名icon"];
    NSArray * titleArr = @[@"",@"设置密码",@"姓名"];
    NSArray * sexImgArr = @[@"男图标",@"女图标"];
    NSArray * bgArr = @[@"男",@"女"];
    sexFlag = @"1";
    for (int i = 0; i<3; i++) {
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(SIZETO6W(29), titleLab.bottom+SIZETO6H(53)+i*(47+SIZETO6H(15)), SCREEN_WIDTH-SIZETO6W(29)*2, 47)];
        view.backgroundColor = [UIColor clearColor];
        view.layer.masksToBounds = YES;
        view.layer.borderColor = [UIColorFromRGB(0xFFFFFF) CGColor];
        view.layer.borderWidth = 1.0f;
        view.layer.cornerRadius = 20;
        [self.view addSubview:view];
        if (i == 0) {
            for (int j = 0; j<2; j++) {
                
                UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
                btn.frame = CGRectMake(j*view.width/2, 0, view.width/2, view.height);
                if (j ==  0) {
                    [btn setBackgroundColor:UIColorFromRGB(0x365631)];
                }
                btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.width/6, 0, -btn.width/6);
                btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.width/6, 0, btn.width/6);
                btn.tag = 20+j;
                [btn setImage:[UIImage imageNamed:sexImgArr[j]] forState:UIControlStateNormal];
                [btn setTitle:bgArr[j] forState:UIControlStateNormal];
                [btn addTarget:self action:@selector(sexAction:) forControlEvents:UIControlEventTouchUpInside];
                [btn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
                [view addSubview:btn];
            }
            UIView * linev = [[UIView alloc]initWithFrame:CGRectMake(view.width/2, 0, 1, view.height)];
            linev.backgroundColor = UIColorFromRGB(0xFFFFFF);;
            [view addSubview:linev];
        }
        else
        {
        
        UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(20), view.height/2-18/2, 15, 20)];
        imgV.image = [UIImage imageNamed:imgArr[i]];
        [view addSubview:imgV];
        
        UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(imgV.right+SIZETO6W(15), 0, view.width-SIZETO6W(20)-SIZETO6W(15)-15, 49)];
        UIColor * color = UIColorFromRGB(0xffffff);
        textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:titleArr[i] attributes:@{NSForegroundColorAttributeName:color}];
        textField.tag = i+100;
        [view addSubview:textField];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 30, 30);
        [btn setImage:[UIImage imageNamed:@"关闭按钮-"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clearBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        textField.rightView = btn;
            [textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
        textField.textColor = color;
        btn.tag = i+10;
        textField.rightViewMode = UITextFieldViewModeWhileEditing;
            if (i == 1) {
                textField.keyboardType = UIKeyboardTypeASCIICapable;
                textField.secureTextEntry = YES;
            }

            if (i == 2) {
                UILabel * tipLab = [[UILabel alloc]initWithFrame:CGRectMake(0, view.bottom+SIZETO6H(13), SCREEN_WIDTH, 20)];
                tipLab.text = @"提示:请填写真实姓名，这样有助于更多优企找到您";
                tipLab.textColor = UIColorFromRGB(0xFFFFFF);
                tipLab.alpha = 0.7;
                tipLab.font = [UIFont systemFontOfSize:13];
                tipLab.textAlignment = NSTextAlignmentCenter;
                [self.view addSubview:tipLab];
                
                UIButton * lastBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                lastBtn.frame = CGRectMake(SIZETO6W(29), tipLab.bottom+SIZETO6H(51), SCREEN_WIDTH-SIZETO6W(29)*2, 47);
                [lastBtn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
                [lastBtn setTitle:@"益企出发" forState:UIControlStateNormal];
                lastBtn.backgroundColor = UIColorFromRGB(0x6ec844);
                lastBtn.layer.masksToBounds = YES;
                lastBtn.layer.cornerRadius = 20;
                [lastBtn addTarget:self action:@selector(lastBtnAction:) forControlEvents:UIControlEventTouchUpInside];
                [self.view addSubview:lastBtn];
                
            }
        
        }
    
    }
}
-(void)textChange:(UITextField *)textField
{
    if (textField.tag == 101) {
        if (textField.text.length > passWordStr.length) {
            if (textField.text.length <= 16) {
                
                NSString * s = [textField.text substringWithRange:NSMakeRange(textField.text.length-1, 1)];
                
                if ([passWordStr isEqualToString:@"&"]) {
                    
                }
                else
                {
                    [passWordStr appendString:s];
                }
                
                
            }
        }
        else if([textField.text isEqualToString:@""])
        {
            passWordStr = [[NSMutableString alloc]init];
        }
        else
        {
            NSString * str = [passWordStr substringToIndex:passWordStr.length-1];
            passWordStr = [[NSMutableString alloc]initWithString:str];
        }
        textField.text = passWordStr;
    }
    else
    {
//        if (textField.text.length > nameStr.length) {
//            if (textField.text.length <= 40) {
//                
//                NSString * s = [textField.text substringWithRange:NSMakeRange(textField.text.length-1, 1)];
//                
//                [nameStr appendString:s];
//                
//            }
//        }
//        else if([textField.text isEqualToString:@""])
//        {
//            nameStr = [[NSMutableString alloc]init];
//        }
//        else
//        {
//            NSString * str = [nameStr substringToIndex:nameStr.length-1];
//            nameStr = [[NSMutableString alloc]initWithString:str];
//        }
//        textField.text = nameStr;
    }
}
-(void)clearBtnClick:(UIButton *)btn
{
    if (btn.tag == 11) {
        UITextField * textField = (UITextField *)[self.view viewWithTag:101];
        textField.text = @"";
        passWordStr = [[NSMutableString alloc]init];

    }
    else
    {
        UITextField * textField = (UITextField *)[self.view viewWithTag:102];
        textField.text = @"";
        
    }
    
}
-(void)resignUserWithusername:(NSString *)username userpwd:(NSString *)userpwd mobile:(NSString *)mobile andCode:(NSString *)code
{
    if (userpwd != nil && username != nil && mobile != nil && code != nil ) {
        
        StartYQViewController * startVC = [[StartYQViewController alloc]init];

        startVC.username = username;
        startVC.mobile = mobile;
        startVC.userpwd = userpwd;
        startVC.code = code;
        
        
        //[self presentViewController:StartNAV animated:YES completion:nil];
        startVC.navigationController.navigationBarHidden = YES;
        
        [self.navigationController pushViewController:startVC animated:YES];
    }
}
#pragma mark - btn的点击事件们
-(void)lastBtnAction:(UIButton *)btn
{
    
    
   // 密码
    UITextField * passwordTextField = (UITextField *)[self.view viewWithTag:101];
    
    //姓名
    UITextField * nameTextField = (UITextField *)[self.view viewWithTag:102];
    
    if (![passwordTextField.text isEqualToString:@""]&&![nameTextField.text isEqualToString:@""] && passwordTextField.text.length >=6) {
        __weak typeof(self)weakSelf = self;
        [U76HONet registerUserWithaction:@"" client:@"123654" username:nameTextField.text userpwd:passwordTextField.text Mobile:_phoneNum sex:sexFlag andDataBlock:^(NSDictionary *dic) {
            //        {
            //            errcode = 0;
            //            errmsg = 8332332637f05bd641a9926ad6a47ed7;
            //        }
            NSString * code = [dic objectForKey:@"errmsg"];
            [weakSelf resignUserWithusername:nameTextField.text userpwd:passwordTextField.text mobile:_phoneNum andCode:code];
        }];
    }
    else if (passwordTextField.text.length < 6)
    {
        [Singleton alerSendMessage:@"6-16位密码"];
    }
    else
    {
        [Singleton alerSendMessage:@"不能为空"];
    }
    
    
    
    
}
-(void)sexAction:(UIButton *)btn
{
    int tag = (int)btn.tag;
    if (tag == 20) {
        sexFlag = @"1";
        [btn setBackgroundColor:UIColorFromRGB(0x365631)];
        UIButton * b = (UIButton *)[self.view viewWithTag:21];
        [b setBackgroundColor:[UIColor clearColor]];
        
    }else
    {
        sexFlag = @"2";
        [btn setBackgroundColor:UIColorFromRGB(0x365631)];
        UIButton * b = (UIButton *)[self.view viewWithTag:20];
        [b setBackgroundColor:[UIColor clearColor]];
    }
}
-(void)leftBtnActoin:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
